package org.wukl.vhr.controller.system.basic;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wukl.vhr.model.Department;
import org.wukl.vhr.model.ResponseBean;
import org.wukl.vhr.service.DepartmentService;

import java.util.List;

@RestController
@RequestMapping("/sys/basic/depart")
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;
    @GetMapping("/")
    public List<Department> queryAllDepartment()
    {
        return departmentService.queryAllDepartment();
    }
    @PostMapping("/")
    public ResponseBean addDepartment(@RequestBody Department addDept)
    {
        departmentService.addDepartment(addDept);
        if(addDept.getBatchResult()==1)
        {
            return ResponseBean.responseSuccess("部门添加成功",addDept);
        }
        return ResponseBean.responseError("部门添加失败");
    }

    /**
     * 删除时需要考虑删除部门下有没有员工和子部门
     * @param id:删除部门id
     * @return ResponseBean
     */
    @DeleteMapping("/{id}")
    public ResponseBean deleteDepartment(@PathVariable Integer id)
    {
        Department depart=new Department();
        depart.setId(id);
        departmentService.deleteDepartment(depart);
        if(depart.getBatchResult()==-2)
        {
            return ResponseBean.responseError("删除部门下有子部门信息，删除失败");
        }
       else if(depart.getBatchResult()==-1)
        {
            return ResponseBean.responseError("删除部门下有员工信息，删除失败");
        }
       else if(depart.getBatchResult()==1)
       {
           return ResponseBean.responseSuccess("部门删除成功");
       }
       return ResponseBean.responseError("部门删除失败");

    }
}
